<?php


namespace app\api\model;

use think\facade\Db;
use think\response\Json;
use \Exception as E;

class DeliveryControModel
{
    private static $_orderTable = 'order'; // 订单表
    private static $_companyTable = 'company'; // 公司表
    private static $_adminTable = 'admin'; // 管理员表
    private static $_orgTable = 'organization'; // 分拣中心表
    private static $_cAddressTable = 'company_address'; // 公司地址表
    /**
     * @param $input
     * @return Json
     */
    public static function get_out_order($input)
    {
        try {
            $types = explode(',', $input['status']);
            $map = [
                ['o.driver_id', '=', $input['driver_id']],
                ['o.center_id', '=', $input['center_id']],
                ['o.status', 'in', $types],
            ];
            $field = 'o.id as order_id,mastr_order_no,order_no,o.entrust_id,o.status as order_status,goods_data,num,';
            $field .= 'or.province as org_province,or.city as org_city,or.area as org_area,or.name as org_name,';
            $field .= 'a.name as admin_name,a.phone as admin_phone,';
            $field .= 'ca.province as cp_province,ca.city as cp_city,ca.area as cp_area,ca.address as cp_address,ca.name as cp_name,ca.phone as cp_phone';
            $info = Db::table(self::$_orderTable)->alias('o')->force('index_c_d_s')
                ->leftJoin([self::$_companyTable => 'c'], 'o.group_id= c.id')
                ->leftJoin([self::$_adminTable => 'a'], 'a.id = o.retrieval_id')
                ->leftJoin([self::$_orgTable => 'or'], 'or.id = o.center_id')
                ->leftJoin([self::$_cAddressTable => 'ca'], 'ca.id = o.address_id')
                ->where($map)
                ->field($field)
                ->select()->toArray();
            $data = [];
            foreach($info as $k => $item){
                $data[$item['mastr_order_no']][] = $item;
            }
            return jsonSuccess('获取成功', ['list' => $data]);
        } catch (E $e) {
            return jsonFail('服务内部错误~');
        }
    }
}